//
//  MainViewController.swift
//  Weibo
//
//  Created by 王少卓 on 16/1/19.
//  Copyright © 2016年 王少卓. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    //MARK: 加号按钮的监听事件
    @objc private func plusButtonClick() {
        print(__FUNCTION__)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        
        //使用KVC 间接修改系统的tabbar
        let mainTabBar = MainTabbar()
        setValue(mainTabBar, forKey: "tabBar")
        
        //给加号按钮添加点击事件
        mainTabBar.plusButton.addTarget(self, action: "plusButtonClick", forControlEvents: .TouchUpInside)
        
        //跟控制器的入口
        addChildViewControllers()
    }
    private func addChildViewControllers() {
        //添加首页按钮
        addChildViewController(HomeTableViewController(), imageNamed: "tabbar_home", title: "首页",selectedImage: "tabbar_home_highlighted")
        //添加消息按钮
        addChildViewController(MessageTableViewController(), imageNamed: "tabbar_message_center", title: "消息",selectedImage: "tabbar_message_center_highlighted")
        //添加发现按钮
        addChildViewController(DiscoverTableViewController(), imageNamed: "tabbar_discover", title: "发现",selectedImage: "tabbar_discover_highlighted")
        //我
        addChildViewController(ProfileTableViewController(), imageNamed: "tabbar_profile", title: "我",selectedImage: "tabbar_profile_highlighted")
    }
    
    // 添加子视图控制器
    private func addChildViewController(vc: UIViewController,imageNamed: String,title: String,selectedImage: String) {
        //实例化 vc
       
        //设置图片
        vc.tabBarItem.image = UIImage(named: imageNamed)
        //设置选中图片 
        vc.tabBarItem.selectedImage = UIImage(named: selectedImage)

        //设置title
        vc .title = title
        //设置渲染
        self.tabBar.tintColor = UIColor.orangeColor()
        //导航控制器
        let nav = UINavigationController(rootViewController: vc)
        //添加子视图控制器
        addChildViewController(nav)
    }
}
